package com.brsya.movie.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.brsya.base.base.BaseMvpFragment;
import com.brsya.base.base.Contents;
import com.brsya.base.bean.InventoryDetailBean;
import com.brsya.base.bean.MovieBean;
import com.brsya.base.bean.RankingListBean;
import com.brsya.base.recycler.BaseAdapter;
import com.brsya.base.util.LoggerUtil;
import com.brsya.movie.R;
import com.brsya.movie.activity.PlayActivity;
import com.brsya.movie.adapter.RankingListAdapter;
import com.brsya.movie.contract.RankingListContract;
import com.brsya.movie.presenter.RankingListPresenter;

import java.util.List;

/**
 * Created by Brsya
 * CreateDate: 2022/7/6  20:52
 * Description:
 * 排行榜列表
 */
public class RankingListFragment extends BaseMvpFragment<RankingListPresenter> implements RankingListContract.View {


    private SwipeRefreshLayout srlRefresh;
    private RecyclerView rlRankingList;


    private String videoType;

    private RankingListAdapter adapter;


    private boolean isFirstLoad = true;

    public static RankingListFragment newInstance(String videoType) {

        Bundle args = new Bundle();
        args.putString("videoType", videoType);

        RankingListFragment fragment = new RankingListFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void setArguments(@Nullable Bundle args) {
        super.setArguments(args);
        if (args != null) {
            videoType = args.getString("videoType");
        }
    }

    @Override
    protected void initView() {
        setContentViewId(R.layout.fragment_ranking_list);

        srlRefresh = findViewById(R.id.srl_refresh);
        rlRankingList = findViewById(R.id.rl_ranking_list);
    }

    @Override
    protected void initData() {

        srlRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                loadRankingList();
            }
        });

        rlRankingList.setLayoutManager(new LinearLayoutManager(getContext()));
        adapter = new RankingListAdapter(getContext());


        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener<RankingListBean>() {
            @Override
            public void onItemClick(int position, RankingListBean data) {
                Intent intent = new Intent(getActivity(), PlayActivity.class);
                MovieBean movieBean = new MovieBean();

                movieBean.setId(data.getId());
                movieBean.setName(data.getName());
                movieBean.setCoverUrl(data.getCoverUrl());
                movieBean.setUpState(data.getUpState());
                movieBean.setDoubanScore(data.getDoubanScore());
                movieBean.setVideoArea(data.getVideoArea());
                movieBean.setVideoYear(data.getVideoYear());
                movieBean.setVideoTag(data.getVideoTag());

                intent.putExtra(Contents.INTENT_MOVIE_DETAIL, movieBean);
                startActivity(intent);
            }
        });

        rlRankingList.setAdapter(adapter);

        View bottomView = LayoutInflater.from(getContext()).inflate(R.layout.item_no_more_data, null, false);
        adapter.setBottomView(bottomView);
    }


    @Override
    public void onResume() {
        super.onResume();
        if (isFirstLoad) {
            presenter.getRankingList(videoType, 0);
            isFirstLoad = false;
        }
    }

    @Override
    protected void initPresenter() {
        presenter = new RankingListPresenter();
        presenter.attachView(this);
    }

    private void loadRankingList() {
        presenter.getRankingList(videoType, 1);
    }

    @Override
    public void showRankingList(List<RankingListBean> rankingListBeans) {
        if (rankingListBeans == null) {
            noMoreRankingList();
            return;
        }
        adapter.setDataList(rankingListBeans);
        adapter.notifyDataSetChanged();
    }

    @Override
    public void noMoreRankingList() {

    }

    @Override
    public void showLoading() {
        super.showLoading();
        srlRefresh.setRefreshing(true);
    }

    @Override
    public void hideLoading() {
        super.hideLoading();
        srlRefresh.setRefreshing(false);
    }
}
